<template>
    <div class="gxgxToplbt">
        <div style="width: 18%;">
            <classification title="分类" :sysProductTypeData="sysProductTypeData" @list-click="spListFun" />
        </div>
        <div style="width: 39%;">
            <div style="height: 190px;margin-bottom: 20px;">
                <RoundCornerCarousel height="190px" :lbtImg="lbtImg.slice(0, 5)" />
            </div>
            <div class="topCardjc">
                <div class="topCardjcTitle">要闻动态</div>
                <div class="xwdtList" v-for="item in ywdtxwData" :key="item" @click="xwDetailsfun(item.id)">
                    <div class="huidian"></div>
                    <div class="hxwdtTextBox">{{ item.title }}</div>
                </div>
            </div>
        </div>
        <div style="width: 39%;">
            <div class="topCardjc" style="margin-bottom: 20px;">
                <div class="topCardjcTitle">推荐商品</div>
                <div class="tjspImgflex">
                    <div class="tjspImgFor" v-for="(item, index) in lbtImg.slice(0, 3)" :key="index">
                        <img style="width: 100%;height: 100%;" :src="item.url" alt="">
                        <div class="tjspImgText">{{ item.name }}</div>
                    </div>
                </div>
            </div>
            <div class="topCardjc">
                <div class="topCardjcTitle">企业推荐</div>
                <div class="qytjFlex">
                    <div class="qytjFor" v-for="(item, index) in enterpriseHotEnterpriseData" :key="index">
                        <div class="qytjIcon">
                            <img style="width: 100%;height: 100%;"
                                :src="item.file_path == 'UpLoadFiles\\qiye.png' ? `https://image.pollinations.ai/prompt/公司大厦${item.id}` : store.imgfengMian + item.file_path"
                                alt="" loading="lazy" > 
                        </div>
                        <div class="qytjText">{{ item.name }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="sptjBox">
        <div class="sptjBoxTabs">
            <div class="sptjBoxTabsFor" :class="{ sptjBoxTabsForXz: parameterData.type === '' }"
                @click="spTablsfun('')">
                <img class="tabsImg" src="/src/assets/icon/分类.png" alt="">
                <span class="tabsText">商品推荐</span>
            </div>
            <div class="sptjBoxTabsFor" v-for="item in sysProductTypeData" :key="item.id"
                :class="{ sptjBoxTabsForXz: parameterData.type === item.dictValue }"
                @click="spTablsfun(item.dictValue)">
                <img class="tabsImg" :src="`${JSON.parse(item.remark).url}${item.dictLabel}.png`" alt="">
                <span class="tabsText">{{ item.dictLabel }}</span>
            </div>
        </div>
        <div class="spListBox">
            <div class="spForBox" v-for="item in queryProductListData" :key="item.id" @click="spxqdzfun(item.id)">
                <img class="spForImgBox" :src="item.logo" alt="" loading="lazy">
                <div class="spForTitle">{{ item.name }}</div>
                <div class="spForText">{{ item.enterprise_name }}</div>
                <div class="spForjgrs">
                    <div class="spForjgFlex">
                        <span style="font-size: 14px;">￥</span>
                        <span style="font-size: 20px;">{{ item.price }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
import classification from '@/components/general/classification.vue';
import RoundCornerCarousel from '@/components/general/RoundCornerCarousel.vue';
import { queryNewsList, queryProductList, enterpriseHotEnterprise } from '@/api/api.js';
import { sysProductType } from '@/api/dictionary.js';
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();
import { useStore } from '@/store/store';
const store = useStore()
const lbtImg = ref([]);
onMounted(() => {
    sysProductTypefun()
    queryProductListfun()
    ywdtxwListfun() // 要闻动态
    enterpriseHotEnterprisefun() // 企业热榜推荐
})
const parameterData = ref({
    pageNum: 1,
    pageSize: 7,
    type: '',
})
const queryProductListData = ref([]);
const queryProductListfun = async () => {
    const res = await queryProductList(parameterData.value);
    if (res.code === 200) {
        queryProductListData.value = res.data.map(item => {
            let url = item.upload.find(a => a.attachment_type === 'Cover')?.file_path || ''
            return {
                ...item,
                logo: url == 'UpLoadFiles\\shop.png' ? `https://image.pollinations.ai/prompt/${item.name.split('、')[0]}` : store.imgfengMian + url
            };
        })
        lbtImg.value = res.data.map(item => {
            let url = item.upload.find(a => a.attachment_type === 'Cover')?.file_path || ''
            return {
                name: item.name,
                url: url == 'UpLoadFiles\\shop.png' ? `https://image.pollinations.ai/prompt/${item.name.split('、')[0]}` : store.imgfengMian + url
            };
        })
    }
}
const spTablsfun = (val) => {
    parameterData.value.type = val;
    queryProductListfun();
}
const sysProductTypeData = ref([]);
const sysProductTypefun = async () => {
    const res = await sysProductType();
    sysProductTypeData.value = res.data;
}
// 要闻动态
const ywdtxwData = ref([]);
const ywdtxwListfun = async () => {
    const res = await queryNewsList({
        page: 1,
        limit: 5,
        new_type: '011'
    })
    ywdtxwData.value = res.data;
}
// 企业热榜推荐
const enterpriseHotEnterpriseData = ref([]);
const enterpriseHotEnterprisefun = async () => {
    const res = await enterpriseHotEnterprise({
        page: 1,
        limit: 4,
    })
    enterpriseHotEnterpriseData.value = res.result
}
// 要闻动态详情
const xwDetailsfun = (id) => {
    router.push({
        path: '/newsDetail',
        query: { id: id }
    });
}
const spListFun = (val) => {
    router.push({
        path: '/goodsList',
        query: { type: val.dictValue }
    });
}
const spxqdzfun = (id) => {
    router.push({
        path: '/ProductDetails',
        query: { id: id }
    });
}
</script>
<style scoped lang="scss">
.gxgxToplbt {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-between;
}

.topCardjc {
    width: 100%;
    height: 190px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1),
        0 8px 16px rgba(0, 0, 0, 0.1); // 添加双层阴影增强立体感
    padding: 10px 20px;
}

.topCardjcTitle {
    font-weight: bold;
    font-size: 16px;
    color: #1F1F1F;
    margin-bottom: 5px;
}

.xwdtList {
    display: flex;
    align-items: center;
    padding: 5px 0;

    &:hover {
        background: #F5F5F5;
        border-radius: 4px;
    }

    .huidian {
        width: 4px;
        height: 4px;
        background: #8C8C8C;
        border-radius: 50%;
        flex-shrink: 0;
        margin-right: 10px;
    }

    .hxwdtTextBox {
        font-weight: 300;
        font-size: 12px;
        color: #1F1F1F;
        white-space: nowrap; // 禁止换行
        overflow: hidden; // 隐藏溢出内容
        text-overflow: ellipsis; // 显示省略号
        cursor: pointer;
    }
}

.tjspImgflex {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 10px;

    .tjspImgFor {
        width: 135px;
        height: 135px;
        border-radius: 4px;
        overflow: hidden;
        position: relative;

        .tjspImgText {
            position: absolute;
            bottom: 0;
            width: 100%;
            font-size: 12px;
            padding: 3px 10px;
            background: rgba(0, 0, 0, 0.5);
            color: rgba(255, 255, 255, 0.8);
        }
    }
}

.qytjFlex {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .qytjFor {
        display: flex;
        align-items: center;
        border-radius: 4px;
        border: 1px solid #DDDDDD;
        width: 49%;
        padding: 10px;
        margin-top: 10px;
        cursor: pointer;

        .qytjIcon {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            overflow: hidden;
            margin-right: 10px;
            flex-shrink: 0;
        }

        .qytjText {
            font-size: 14px;
            color: #1F1F1F;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2; // 限制两行
            overflow: hidden;
            text-overflow: ellipsis;
            // line-height: 1.5; // 新增行高设置
            // height: 42px; // 两行高度（14px * 1.5 * 2）
        }
    }
}

.sptjBox {
    margin-top: 20px;

    .sptjBoxTabs {
        display: flex;
        align-items: center;
        justify-content: space-between;

        .sptjBoxTabsFor {
            padding: 5px 15px;
            display: flex;
            align-items: center;
            border-radius: 4px;
            cursor: pointer;

            .tabsImg {
                width: 18px;
                height: 18px;
                margin-right: 10px;
            }

            .tabsText {
                font-size: 14px;
                color: #1F1F1F;
            }
        }

        .sptjBoxTabsForXz {
            background: #C5E5CA;
        }
    }

    .spListBox {
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;
        gap: 10px;

        .spForBox {
            width: calc(20% - 8px); // 5列布局（100% / 5 = 20%）
            flex-shrink: 0;
            padding: 10px;
            background: #fff;
            border-radius: 12px;
            cursor: pointer;

            .spForImgBox {
                width: 100%;
                height: 200px;
            }

            .spForTitle {
                font-size: 16px;
                color: #333333;
                margin-bottom: 5px;
                font-weight: bold;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                max-width: 100%;
            }

            .spForText {
                font-size: 14px;
                color: #333333;
                margin-bottom: 5px;
                white-space: nowrap; // 禁止换行
                overflow: hidden; // 隐藏溢出内容
                text-overflow: ellipsis; // 显示省略号
            }


            .spForjgrs {
                display: flex;
                align-items: flex-end;

                .spForjgFlex {
                    display: flex;
                    align-items: flex-end;
                    color: #EF1F1F;
                    font-weight: bold;
                }
            }
        }
    }
}
</style>